package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.slog.annotation.SLog;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.groupbuy.services.GoodsService;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.Goods;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Goods module class
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/goods")
public class GoodsModule {

    private static final Log logger = Logs.get();

    @Inject
    private GoodsService goodsService;

    @At
    @Ok("beetl:admin/goods.jsp")
    public void index(HttpServletRequest req) {
        req.setAttribute("sum", goodsService.sumGoods());
    }

    @POST
    @At
    @SLog(module = Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_ADD, content="新增【${args[0].goodsName}】商品信息")
    public AjaxReturn add(@Param("..") Goods goods) {
        AjaxReturn ar = Ajax.ok();
        boolean flag = goodsService.add(goods);
        if (!flag) {
            ar.setOk(false);
            ar.setMsg("Add new goods failure!!!");
        }
        return ar;
    }

    @POST
    @At
    public Map<String, Integer> getGoodsSum() {
        return goodsService.sumGoods();
    }

    @POST
    @At
    @SLog(module = Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_UPDATE, content="更新【${args[0].goodsName}】商品信息")
    public AjaxReturn update(@Param("..") Goods goods) {
        AjaxReturn ar = Ajax.ok();
        boolean flag = goodsService.update(goods);
        if (!flag) {
            ar.setOk(false);
            ar.setMsg("Update goods failure!!!");
        }
        return ar;
    }

    @POST
    @At
    public Map<String, Object> dataList(int page, int rows,
                                        @Param("goodsName") String goodsName, @Param("cId") int companyId) {
        return goodsService.getData(page, rows, goodsName, companyId);
    }
}
